<template>
    <a :href='link'>{{ linkName }}<slot></slot></a>
</template>
<script setup lang="ts">
import { withBase } from 'vitepress';
import { simpleGet } from './common';
</script>
<script lang="ts">

// 使用 slot 实现主要是为了兼容 gitee 的 md 显示
export default {
    props: {
        anchor: {
            type: String,
            default: ''
        },
        mainUrl: {
            type: String,
            default: withBase('/api/all.html')
        },
        name: String
    },
    computed: {
        linkName() : string {
            if(this.$slots.default) {
                return '';
            }
            return this.name ? this.name : this.anchor;
        },
        link() : string {
            let anchor = this.anchor ? this.anchor : simpleGet(this.$slots.default(), '0.children', '');

            return `${this.mainUrl}#${anchor.toLowerCase()}`;
        }
    }
}
</script>